package cn.xiangxu.flux_server.controller;

import cn.xiangxu.flux_server.domain.domain_po.StudentPO;
import cn.xiangxu.flux_server.repository.StudentRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.print.DocFlavor;

/**
 * @author 王杨帅
 * @create 2018-08-21 22:12
 * @desc 学生控制层
 **/
@RestController
@RequestMapping(value = "/stu")
@Slf4j
public class StudentController {

    @Autowired
    private StudentRepository studentRepository;

    /**
     * 新增用户数据
     * @param studentPO
     * @return
     */
    @PostMapping
    public Mono<ResponseEntity<String>> createStudent(
            @RequestBody StudentPO studentPO
    ) {
        log.info("获取到的参数信息为：" + studentPO);

        // 如果请求数据中包含了ID就抛出异常
        if (!StringUtils.isEmpty(studentPO.getId())) {
            // 注意：在实际开发中应该记录日志，并抛出异常；在处理异常那里在决定向前台返回什么
            return Mono.just(new ResponseEntity<String>("新增用户时不能设置ID", HttpStatus.OK));
        }

        return studentRepository.save(studentPO)
                .map(info -> new ResponseEntity<String>("新增用户成功", HttpStatus.OK));

    }

    /**
     * 删除操作
     * @param studentId
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Mono<ResponseEntity<String>> deleteStudent(
            @PathVariable(value = "id") String studentId
    ) {
        log.info("获取到的参数信息： studentId = " + studentId);

        if (StringUtils.isEmpty(studentId)) {
            // 注意：在实际开发中应该记录日志，并抛出异常；在处理异常那里在决定向前台返回什么
//            throw new RuntimeException("学生ID不能为空");
            return Mono.just(new ResponseEntity<String>("学生ID不能为空", HttpStatus.OK));
        }

        return studentRepository.findById(studentId)
                .flatMap(info -> studentRepository
                                    .delete(info)
                                    .then(Mono.just(new ResponseEntity<String>("删除成功", HttpStatus.OK)))

                )
                .defaultIfEmpty(new ResponseEntity<String>("ID不存在", HttpStatus.OK));

    }

    /**
     * 修改
     * @param studentPO
     * @return
     */
    @PutMapping
    public Mono<ResponseEntity<String>> updateStudent(
            @RequestBody StudentPO studentPO
    ) {

        // 如果没有ID就抛出异常
        if (StringUtils.isEmpty(studentPO.getId())) {
            // 注意：在实际开发中应该记录日志，并抛出异常；在处理异常那里在决定向前台返回什么
//            throw new RuntimeException("学生ID不能为空");
            return Mono.just(new ResponseEntity<String>("学生ID不能为空", HttpStatus.OK));
        }

        return studentRepository.findById(studentPO.getId())
                .flatMap(info -> studentRepository.save(studentPO)) // ID合法就进行更新操作
                .map(info -> new ResponseEntity<String>("学生数据修改成功", HttpStatus.OK))
                .defaultIfEmpty(new ResponseEntity<String>("学生ID不存在", HttpStatus.OK)); // ID不合法就返回提示信息

    }

    @GetMapping
    public Flux<StudentPO> listStudent() {
        return studentRepository.findAll();
    }

    @GetMapping(value = "/{id}")
    public Mono<ResponseEntity<StudentPO>> getStudent(
            @PathVariable(value = "id") String studentId
    ) {
        log.info("获取到的 studentId = " + studentId);

        return studentRepository.findById(studentId)
                .map(info -> new ResponseEntity<StudentPO>(info, HttpStatus.OK))
                .defaultIfEmpty(
                    new ResponseEntity<StudentPO>(StudentPO.builder().build() ,HttpStatus.OK)
                );

    }



}

